﻿using Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLine.Models;

namespace ZoneLine.Stations.XWStation
{
    public abstract class XWStationBase : WorkStationDBase
    {
        public void Init(string ip, int port = 102, string startAddr = "DB55.0", string endUploadCodeAddr = "DB55.18.1", string endUploadDataAddr = "DB55.18.2", string allowWorkAddr = "DB55.20")
        {
            Ip = ip;
            Port = port;
            _startAddr = startAddr;
            _readLength = 126;

            EndUpLoadCodeAddr = endUploadCodeAddr;
            EndUpLoadDataAddr = endUploadDataAddr;
            AllowWorkAddr = allowWorkAddr;
            HeartToPlcAddr = "DB55.18.0";

            StationRecordData = new HOP010StationResult();
        }

        private HStationResultBase _stationRecordData;

        public HStationResultBase StationRecordData
        {
            get { return _stationRecordData; }
            set => SetProperty(ref _stationRecordData, value);
        }

        protected override void DealWithSignal(string name)
        {
            if (name == nameof(StartUploadCode))
            {
                //todo 上传了条码 这时候验证是否要工作
                if (StartUploadCode)
                {
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}工位结论验证,条码信息是{StationRecordData.AbsScanCode}");
                    var validataRlt = Validata(CurrentStationName, StationRecordData.AbsScanCode);
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}工位的验证结果是{validataRlt}");
                    var writeRlt = WriteWorkActionToPlc(AllowWorkAddr, validataRlt, StationRecordData.AbsScanCode);
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}工位向下位机下发消息，下发{writeRlt}");
                    return;
                }
                else
                {
                    WriteData(EndUpLoadCodeAddr, false);
                    WriteData(AllowWorkAddr, 0);
                    return;
                }
            }

            if (name == nameof(StartUploadData))
            {
                if (StartUploadData)
                {
                    //todo 上传数据 这时候将数据记录到数据库中
                    RecordData();
                    WriteData(EndUpLoadDataAddr, true);
                }
                else
                {
                    WriteData(EndUpLoadDataAddr, false);
                }
            }
        }

        protected override void DealData(byte[] content)
        {
            StationRecordData.AbsScanCode = GetPlcString(content, 26);
            StationRecordData.BushScanCode = GetPlcString(content, 58);
            StationRecordData.PartType = GetPlcString(content, 90);
            StationRecordData.Result = GetPlcString(content, 102);
            StationRecordData.PressPosition = _client.ByteTransform.TransSingle(content, 114);
            StationRecordData.PressForce = _client.ByteTransform.TransSingle(content, 118);
            StationRecordData.CycleTime = _client.ByteTransform.TransSingle(content, 122);
        }

        protected override WorkActionEnum Validata(string stationName, string code, string[] otherStations = null)
        {
            var currentStationName = stationName;
            var stationIndex = Cache.WorkStationValidationList.IndexOf(currentStationName);
            if (stationIndex == -1)
            {
                return WorkActionEnum.NoEnable;
            }
            LogWithUi(_logUiChannel, $"开始检查是否在本工位{currentStationName}结论");
            var currentResult = CheckCodeExistAndResult(currentStationName, code);
            LogWithUi(_logUiChannel, $"在本工位{currentStationName}工位检查{code}的结论是{currentResult}");
            if (currentResult == 1)
            {
                return WorkActionEnum.HasWorkInCurrent;
            }
            else
            {
                return WorkActionEnum.StartWork;
            }
        }
    }
}
